cmake_minimum_required(VERSION 3.10)

set(PROJECT_NAME ukui-monitor-overlay)
set(PROJECT_VERSION "0.0.2")

project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找 Qt
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

set(PROJECT_SOURCES
    src/main.cpp
    src/monitor.cpp
    src/monitor_settings.cpp
    src/gpumonitorfactory.cpp
    src/nvidiagpumonitor.cpp
    src/amdgpumonitor.cpp
    src/intelgpumonitor.cpp
    src/mttgpumonitor.cpp
    src/pluginmanager.cpp
    src/cpumonitor.cpp
    src/memmonitor.cpp
    include/monitor.h
    include/monitor_settings.h
    include/gpumonitorfactory.h
    include/gpu_monitor_interface.h
    include/nvidiagpumonitor.h
    include/amdgpumonitor.h
    include/intelgpumonitor.h
    include/mttgpumonitor.h
    include/systeminfoplugininterface.h
    include/pluginmanager.h
    include/cpumonitor.h
    include/memmonitor.h
)




if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_SOURCES})
else()
    add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${RESOURCES})
endif()

set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIR})

# 处理平台相关配置
file(READ "/etc/os-release" os_release)
string(REGEX MATCH "ID=[^\n]*" os_id ${os_release})
string(REPLACE "ID=" "" os_id ${os_id})

message(STATUS "Operating System: ${os_id}")

# openKylin
if("${os_id}" STREQUAL "openkylin")
    message(STATUS "Building for openKylin")
    find_package(PkgConfig REQUIRED)

    # 依赖libkysdk-hardware libkysdk-hardware-dev
    pkg_check_modules(KYSDKHARDWARE REQUIRED kysdk-hardware)
    target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKHARDWARE_INCLUDE_DIRS})
    target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKHARDWARE_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} PRIVATE ${KYSDKHARDWARE_LIBRARIES})

    # 依赖sudo apt install libkysdk-qtwidgets libkysdk-qtwidgets-dev
    pkg_check_modules(KYSDKQTWIDGETS_PKG kysdk-qtwidgets)
    target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_INCLUDE_DIRS})
    target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_LIBRARY_DIRS})
    # target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${KYSDKQTWIDGETS_PKG_LIBRARIES})

    # 依赖libkysdk-waylandhelper libkysdk-waylandhelper-dev
    pkg_check_modules(KYSDKWAYLANDHELPER_PKG kysdk-waylandhelper)
    target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKWAYLANDHELPER_PKG_INCLUDE_DIRS})
    target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKWAYLANDHELPER_PKG_LIBRARY_DIRS})

    # 依赖libkf5windowsystem
    find_package(KF5WindowSystem REQUIRED)
    target_link_libraries(${PROJECT_NAME} PRIVATE KF5::WindowSystem)


    target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
        ${KYSDKQTWIDGETS_PKG_LIBRARIES}
        ${KYSDKWAYLANDHELPER_PKG_LIBRARIES})


    target_compile_definitions(${PROJECT_NAME} PRIVATE OPENKYLIN)





# 银河麒麟 V10
elseif("${os_id}" STREQUAL "kylin")
    message(STATUS "Building for Kylin V10")
    # 待添加银河麒麟依赖
    target_compile_definitions(${PROJECT_NAME} PRIVATE KYLIN)
else()
    message(FATAL_ERROR "Unsupported OS. This project can only be built on openKylin or Kylin OS.")
endif()

# 如果是 Qt6，则需要调用 finalize
if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${PROJECT_NAME})
endif()

install(TARGETS ${PROJECT_NAME} DESTINATION bin)
# 将desktop文件安装到/usr/share/applications
install(FILES ./ukui-monitor-overlay.desktop DESTINATION /usr/share/applications)
# /etc/xdg/autostart/
install(FILES ./ukui-monitor-overlay.desktop DESTINATION /etc/xdg/autostart/)
